package com.qf.smartplatform.service.impl;

import com.qf.smartplatform.cache.SceneCache;
import com.qf.smartplatform.constans.ResultCode;
import com.qf.smartplatform.exceptions.*;
import com.qf.smartplatform.mapper.SysSceneMapper;
import com.qf.smartplatform.pojo.BaseUser;
import com.qf.smartplatform.pojo.SysScene;
import com.qf.smartplatform.pojo.SysUserInfo;
import com.qf.smartplatform.service.SysSceneService;
import com.qf.smartplatform.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class SysSceneServiceImpl implements SysSceneService {
    private SceneCache sceneCache;
    @Autowired
    public void setSceneCache(SceneCache sceneCache) {
        this.sceneCache = sceneCache;
    }
    private SysSceneMapper sysSceneMapper;
    @Autowired
    public void setSysSceneMapper(SysSceneMapper sysSceneMapper) {
        this.sysSceneMapper = sysSceneMapper;
    }
    @Override
    public String addScene(SysScene sysScene) {
//        SysUserInfo user = (SysUserInfo) ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getSession().getAttribute("user");
//        Assert.notNull(user,()->{
//            throw  new UnLoginException("未登录，请先登录", ResultCode.NOT_LOGIN);
//        });
        BaseUser loginUser = SecurityUtils.getUser();
        sysScene.setCreateBy(loginUser.getuId());
        sysScene.setCreateTime(new Date());
        sysScene.setStatus(0L);
        int result = sysSceneMapper.addScene(sysScene);
        if (result==0){
            throw new AddException("添加失败",ResultCode.INSERT_FAIL);
        }
        sceneCache.refresh(loginUser.getuId());
        return "添加成功";
    }

    @Override
    public String deleteSceneById(String sceneId) {
        //理论上删除只能删除用户自己下面的场景,同时删除场景后该场景下的所有的设备应当移动到默认场景下
        BaseUser loginUser = SecurityUtils.getUser();



        int result = sysSceneMapper.deleteSceneById(sceneId);
        if (result==0){
            throw new DeleteDataException("删除失败",ResultCode.DELETE_FAIL);
        }



        sceneCache.refresh(loginUser.getuId());

        return "删除成功";
    }

    @Override
    public String updateScene(SysScene sysScene) {
        SysScene scene = sysSceneMapper.findById(sysScene.getSceneId());
        Assert.notNull(scene,()->{
            throw new FindDataException("没有找到符合更新条件的数据",ResultCode.DATA_NOT_EXIST);
        });
        int result= sysSceneMapper.updateScene(sysScene);


        BaseUser loginUser = SecurityUtils.getUser();
        sceneCache.refresh(loginUser.getuId());


        if (result==0){
            throw new UpdateDateException("更新失败",ResultCode.UPDATE_FAIL);
        }
        return "更新成功";
    }
    @Override
    public SysScene findById(Long sceneId) {
        BaseUser loginUser = SecurityUtils.getUser();
        List<SysScene> sysScenes = sceneCache.get(loginUser.getuId());
        Assert.notNull(sysScenes,()->{
            throw new FindDataException("当前场景不存在",ResultCode.DATA_NOT_EXIST);
        });
        List<SysScene> scene = sysScenes.stream().filter(sysScene -> sysScene.getSceneId().equals(sceneId)).collect(Collectors.toList());
        Assert.isTrue(scene.size()==1,()->{
            throw new FindDataException("当前场景不存在",ResultCode.DATA_NOT_EXIST);
        });
        return scene.get(0);
    }

    @Override
    public List<SysScene> findAllByUSer() {
        BaseUser loginUser = SecurityUtils.getUser();
        List<SysScene> sysScenes = sceneCache.get(loginUser.getuId());
        return  sysScenes;
    }
}
